<%-- 
    Document   : roles
    Created on : 1/06/2011, 12:20:37 AM
    Author     : Fernando
--%>

<%@page import="controller.UsuariosRolesMgr"%>
<%@page import="model.UsuariosRoles"%>
<%@page import="controller.RolesMgr"%>
<%@page import="java.util.ArrayList"%>
<%@page import="controller.EmpresasMgr"%>
<%@page import="model.Empresas"%>
<%@page import="java.util.List"%>
<%@page import="model.Roles"%>
<%@page import="controller.UsuariosMgr"%>
<%@page import="model.Usuarios"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
            HttpSession currentSession = request.getSession();
            String doc = currentSession.getAttribute("id") == null ? "" : (String) currentSession.getAttribute("id");
            String html = "";

            if ( doc == null || doc.isEmpty()) {
                response.sendRedirect("/GGP/index.jsp");
            } else {


            Usuarios user = UsuariosMgr.mgr.getUsuario(doc);
            Empresas miEmpresa=EmpresasMgr.mgr.getItemForNit(user.nitEmpresa);

            String opt = request.getParameter("opt") == null ? "" : request.getParameter("opt");

             ArrayList<Roles> lstRolesActual = RolesMgr.mgr.getListPorUsuario(doc);
             Roles rolActual = new Roles();
             if (lstRolesActual.size() > 0) {
                    rolActual = lstRolesActual.get(0);
              }

            String alert = "";

       

                String opc = request.getParameter("opc") == null ? "" : request.getParameter("opc");
                String editar = "";

                    String idRol = request.getParameter("idRol") == null ? "" : request.getParameter("idRol");
                    Roles data = RolesMgr.mgr.getItemForId(idRol);

                    if (opc.equals("eliminar")) {
                        RolesMgr.mgr.eliminarRoles(idRol);
                    }

                    if (opc.equals("guardar")) {

                        String edit = request.getParameter("editar") == null ? "" : request.getParameter("editar");

                        data.nombreRol = request.getParameter("nombreRol") == null ? "" : request.getParameter("nombreRol");
                        data.permisos = request.getParameter("permisos") == null ? "" : request.getParameter("permisos");
                        data.nitEmpresa = user.nitEmpresa;

                        RolesMgr.mgr.execute(data.getUpdateSql2(edit.equals("1")));
                    }

                    ArrayList<Roles> lst = RolesMgr.mgr.getListForEmpresa(user.nitEmpresa);

                    html = "<table class='sortable' cellpadding='3' cellspacing='0'>"
                            + "<thead><tr>"
                            + "<th>NOMBRE</td>"
                            + "<th>PERMISOS</td>"
                            + "<th class='nosort'><img border='0' src='../images/edit.png' /></td>"
                            + "<th class='nosort'><img border='0' src='../images/12.png' /></td></tr></thead><tbody>";

                    for (Roles it : lst) {
                        html += "<tr>" + it.getHtmlRow();

                        html += "<td align='center'><form title='' method='post' action='roles.jsp' >"
                                + "<input type='hidden' name='idRol' value='" + it.idRol + "' />"
                                + "<input type='hidden' name='opc' value='editar' />"
                                + "<input type='hidden' name='opt' value='roles' />"
                                + "<input type='image' border='0' title='editar' src='../images/edit.png' /></form></td>";

                        html += "<td align='center'><form title='' method='post' action='roles.jsp' target='_self'>"
                                + "<input type='hidden' name='idRol' value='" + it.idRol + "' />"
                                + "<input type='hidden' name='opc' value='eliminar' />"
                                + "<input type='hidden' name='opt' value='roles' />"
                                + "<input type='image' border='0' title='eliminar' src='../images/12.png' /></form></td></tr>";
                    }

                    html += "</tbody><tfoot><tr><td align='right' colspan='4'><form title='Agregar Roles' method='post' action='roles.jsp'>"
                            + "<input type='hidden' name='opc' value='agregar' />"
                            + "<input type='hidden' name='opt' value='roles' />"
                            + "<input type='image' border = '0' title='agregar' src='../images/11.png' />"
                            + "</form></td></tr></tfoot></table>";

                    if (opc.equals("agregar") || opc.equals("editar")) {

                        editar = "";
                        if (!data.idRol.equals("")) {
                            editar = "1";
                        }

                        html += "<br><br><form method='post' name='formAdd' target='_self' action='roles.jsp' onsubmit='return validarFormRol()' >"
                                + "<table width='' cellpadding='3' cellspacing='0'><input type='hidden' name='opc' value='guardar' />"
                                + "<input type='hidden' name='opt' value='roles' />"
                                + "<input type='hidden' name='idRol' value='" + idRol + "' />"
                                + "<input type='hidden' name='editar' value='" + editar + "' />"
                                + "<tr><td colspan='2' class='st1'>Agregar Rol</td></tr>";
                        html += "<tr><td class='st3'>Nombre :</td><td class='st4'><input type='text' id='nombreRol' name='nombreRol' value ='" + data.nombreRol + "' size='50' /></td></tr>"
                                + "<tr><td class='st3'>Permisos (*):</td><td class='st4'><select  name='permisos' id='permisos' value = '" + data.permisos + "' ><option value='0' >Consultor</option><option value='-1' >Ninguno</option></select></td></tr>"
                                + "<tr><td class='st3' colspan='2' align='right'><input type='image' onclick='return formAdd_validar()' src='../images/10.png'></td></tr>"
                                + "</table></form>";
                    }

                

      

%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Merchant Manager</title>
        <link href="../main.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="../js/prototype.js"></script>
        <script type="text/javascript" src="../js/tablekit.js"></script>
        <script type="text/javascript">
            function validarFormRol() {

                if ($F('nombreRol')=="") {
                    $('nombreRol').focus();
                    alert('El campo es obligatorio');
                    return false;
                }
                return true;
            }
         </script>
    </head>
    <body>
<div id="page">
	<div id="header">
			<h1>Merchant Manager</h1>
			<div class="description">Usuario: <%=user.nombre%> / Empresa: <%=miEmpresa.nombre%> / Rol: <%=rolActual.nombreRol%> </div>
	</div>

	<div id="mainarea">
	<div id="sidebar">
		<div id="sidebarnav">

                <a href="empresas.jsp">Empresas</a>
                <a class="active" href="roles.jsp">Roles</a>
                <a href="empleados.jsp">Empleados</a>
                <a href="../cerrar.jsp">Cerrar Sesión</a>
                <a href="../ayuda.jsp">Ayuda</a>
	</div>
</div>

<div id="contentarea">


 <%=html%>
               
</div>
</div>
<div id="footer">
Copyright &copy; 2011 - UNDeveloper 
</div>
</div>
</body>
</html>
<%
}
%>